﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace SudokuWPF.Converter
{
    //颜色轻盈化转换器
    [ValueConversion(typeof(Color), typeof(Color))]
    public class ColorLightnessConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            int param = int.Parse(parameter.ToString());

            Color src = (Color)value;
            Color ret = new Color();
            ret.A = src.A;
            ret.R = (byte)Math.Max(Math.Min(src.R + param, 255), 0);
            ret.G = (byte)Math.Max(Math.Min(src.G + param, 255), 0);
            ret.B = (byte)Math.Max(Math.Min(src.B + param, 255), 0);
            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            int param = int.Parse(parameter.ToString());

            Color src = (Color)value;
            Color ret = new Color();
            ret.A = src.A;
            ret.R = (byte)Math.Max(Math.Min(src.R - param, 255), 0);
            ret.G = (byte)Math.Max(Math.Min(src.G - param, 255), 0);
            ret.B = (byte)Math.Max(Math.Min(src.B - param, 255), 0);
            return ret;
        }
    }
}
